package com.biostime.dingdingbox.modules.db.service;

import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.biostime.dingdingbox.common.service.CrudService;
import com.biostime.dingdingbox.common.utils.StringUtils;
import com.biostime.dingdingbox.modules.db.dao.GameStrategyDao;
import com.biostime.dingdingbox.modules.db.dto.GameStrategyDto;
import com.biostime.dingdingbox.modules.db.entity.Album;
import com.biostime.dingdingbox.modules.db.entity.GameStrategy;
import com.biostime.dingdingbox.modules.sys.entity.User;

/**
 * 玩法攻略表Service
 * @author chenweicheng
 * @version 2016-06-29
 */
@Service
@Transactional
public class GameStrategyService extends CrudService<GameStrategyDao, GameStrategy> {
	
	@Autowired
	private AlbumService albumService;
	
	public GameStrategy apiGetRecomStrategy(String boxId){
		return dao.apiGetRecomStrategy(boxId);
	}
	
	public List<GameStrategyDto> apiGetMoreStrategy(String boxId){
		return dao.apiGetMoreStrategy(boxId);
	}
	
	public GameStrategyDto apiGetStrategy(String strategyId){
		return dao.apiGetStrategy(strategyId);
	}
	
	public int apiUpdateRecommended(String boxId){
		return dao.apiUpdateRecommended(boxId);
	}
	
	/**
	 * 新增/修改玩法攻略 by 后台
	 * @author chenweicheng
	 * @version 2016-07-05
	 * @param gameStrategy
	 * @param request
	 */
	public void saveGameStrategyByWeb(GameStrategy gameStrategy , HttpServletRequest request){
		if(gameStrategy != null){
			gameStrategy.setCreateBy(new User("1"));
			gameStrategy.setCreateDate(new Date());
			gameStrategy.setUpdateBy(new User("1"));
			gameStrategy.setUpdateDate(new Date()); 
			
			//修改该攻略所属魔法盒所有的攻略的状态为不推荐
			if(gameStrategy.getIsRecommended() == 1){
				apiUpdateRecommended(String.valueOf(gameStrategy.getBoxId()));
			}
			
		}
			
		//新增图片记录
		Album cla = new Album();
		cla.setCategory("gameStrategy");
		cla.setObjectId(StringUtils.toInteger(gameStrategy.getId()));
		albumService.deleteAll(cla);
		
		String[] photos = request.getParameterValues("hid_photo_name");
		String[] remarks = request.getParameterValues("hid_photo_remark");
		if(photos!=null){
			for(int i=0;i<photos.length;i++){
				String photo = photos[i];
				String remark = remarks[i];
				
				if(!StringUtils.isEmpty(photo)){
					String[] parts = photo.split("\\|");
					if(parts!=null && parts.length>=6){
						Album album = new Album();
						album.setObjectId(StringUtils.toInteger(gameStrategy.getId()));
						album.setCategory(parts[0]);
						album.setFileName(parts[1]);
						album.setOrginPath(parts[2]);
						album.setThumbPath(parts[3]);
						album.setAlbumSize(StringUtils.toInteger(parts[4]));
						album.setRemarks(remark);
						albumService.save(album);
						
						//设置封面图片
						gameStrategy.setCover(parts[2]); 
					}
				}
			}
		}	
		
		save(gameStrategy);		
		
	}
}
